package cn.lastjune.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.lastjune.dao.impl.AreaDaoImpl;

@Controller
@RequestMapping("/pivot")
public class PivotGrid {

	@Autowired
	AreaDaoImpl dao;

	@RequestMapping(method = RequestMethod.POST)
	public void main(HttpServletRequest request, HttpServletResponse response)
			throws IOException, SQLException {
		PrintWriter out = response.getWriter();
		List<Object[]> list = dao.getRate();
		Iterator it = list.iterator();
		String jsonHead = "{" + "metaData:{" + "totalProperty : 'total',"
				+ "root : 'result'," + "fields: ['happenArea','rate']},"
				+ "total:" + list.size() + ", result : [";
		StringBuilder sb2 = new StringBuilder();
		sb2.append(jsonHead);
		// String[] tableName = {"Area"};
		// Class[] className = {Area.class};
		int count = list.size();
		for (Object[] o : list) {
			count--;
			sb2.append("{" + "happenArea : '" + o[0].toString() + "',"
					+ "rate : '" + o[1].toString() + "'" + "}");
			if (count > 0) {
				sb2.append(",");
			}
		}
		sb2.append("]}");
		System.out.println(sb2);
		out.print(sb2);
		out.flush();
	}

//	@RequestMapping(method = RequestMethod.GET)
//	public void excel(HttpServletRequest request, HttpServletResponse response)
//			throws IOException, SQLException {
//		List<Object[]> list = dao.getRate();
//		Iterator it = list.iterator();
//		String jsonHead = "{" + "metaData:{" + "totalProperty : 'total',"
//				+ "root : 'result'," + "fields: ['happenArea','rate']},"
//				+ "total:" + list.size() + ", result : [";
//		StringBuilder sb2 = new StringBuilder();
//		sb2.append(jsonHead);
//		// String[] tableName = {"Area"};
//		// Class[] className = {Area.class};
//		int count = list.size();
//		List datas = new ArrayList();
//		for (Object[] o : list) {
//			String[] data = { o[0].toString(), o[1].toString() };
//			datas.add(data);
//		}
//		// 写入excel
//		try {
//			OutputStream out2 = new FileOutputStream(new File(
//					"c:\rate.xls"));
//			ExcelUtil jxlExcelWriter = new ExcelUtil();
//			jxlExcelWriter.writeExcel(out2, datas, new String[] { "Id", "name" });
//			out2.close();
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//
//	}

}
